<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="当事人类型，1:自然人；2:法人；3:非法人组织" prop="dsrlx">
        <el-select v-model="queryParams.dsrlx" placeholder="请选择当事人类型，1:自然人；2:法人；3:非法人组织" clearable>
          <el-option
            v-for="dict in dict.type.fs_gw_dsrlx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="xm">
        <el-input
          v-model="queryParams.xm"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="dz">
        <el-input
          v-model="queryParams.dz"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="dwmc">
        <el-input
          v-model="queryParams.dwmc"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职业" prop="zy">
        <el-select v-model="queryParams.zy" placeholder="请选择职业" clearable>
          <el-option
            v-for="dict in dict.type.fs_gw_zy"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位性质" prop="dwxz">
        <el-select v-model="queryParams.dwxz" placeholder="请选择单位性质" clearable>
          <el-option
            v-for="dict in dict.type.fs_gw_dwxz"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证照号码" prop="zzh">
        <el-input
          v-model="queryParams.zzh"
          placeholder="请输入证照号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要办事机构所在地" prop="zybsjgszd">
        <el-input
          v-model="queryParams.zybsjgszd"
          placeholder="请输入主要办事机构所在地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="zjhm">
        <el-input
          v-model="queryParams.zjhm"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="cjsj">
        <el-input
          v-model="queryParams.cjsj"
          placeholder="请输入创建时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fs:client:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fs:client:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fs:client:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fs:client:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="当事人类型，1:自然人；2:法人；3:非法人组织" align="center" prop="dsrlx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fs_gw_dsrlx" :value="scope.row.dsrlx"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="xm" />
      <el-table-column label="地址" align="center" prop="dz" />
      <el-table-column label="证照号码" align="center" prop="zzh" />
      <el-table-column label="主要办事机构所在地" align="center" prop="zybsjgszd" />
      <el-table-column label="法定代表人姓名" align="center" prop="fddbr" />
      <el-table-column label="证件号码" align="center" prop="zjhm" />
      <el-table-column label="创建时间" align="center" prop="cjsj" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fs:client:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fs:client:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改当事人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="本地数据库id" prop="localId">
          <el-input v-model="form.localId" placeholder="请输入本地数据库id" />
        </el-form-item>
        <el-form-item label="标号" prop="bh">
          <el-input v-model="form.bh" placeholder="请输入标号" />
        </el-form-item>
        <el-form-item label="当事人类型，1:自然人；2:法人；3:非法人组织" prop="dsrlx">
          <el-select v-model="form.dsrlx" placeholder="请选择当事人类型，1:自然人；2:法人；3:非法人组织">
            <el-option
              v-for="dict in dict.type.fs_gw_dsrlx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="xm">
          <el-input v-model="form.xm" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="xb">
          <el-select v-model="form.xb" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="nl">
          <el-input v-model="form.nl" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="出生日期" prop="csrq">
          <el-input v-model="form.csrq" placeholder="请输入出生日期" />
        </el-form-item>
        <el-form-item label="地址类型，1:经常居住地；2:户籍所在地" prop="dzlx">
          <el-select v-model="form.dzlx" placeholder="请选择地址类型，1:经常居住地；2:户籍所在地">
            <el-option
              v-for="dict in dict.type.fs_gw_dzlx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="dz">
          <el-input v-model="form.dz" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="单位名称" prop="dwmc">
          <el-input v-model="form.dwmc" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="职业" prop="zy">
          <el-select v-model="form.zy" placeholder="请选择职业">
            <el-option
              v-for="dict in dict.type.fs_gw_zy"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职业名称" prop="zymc">
          <el-input v-model="form.zymc" placeholder="请输入职业名称" />
        </el-form-item>
        <el-form-item label="工作单位" prop="gzdw">
          <el-input v-model="form.gzdw" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="单位性质" prop="dwxz">
          <el-select v-model="form.dwxz" placeholder="请选择单位性质">
            <el-option
              v-for="dict in dict.type.fs_gw_dwxz"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证照类型，4:统一社会信用代码；2：组织机构；5:无" prop="zzlx">
          <el-select v-model="form.zzlx" placeholder="请选择证照类型，4:统一社会信用代码；2：组织机构；5:无">
            <el-option
              v-for="dict in dict.type.fs_gw_zzlx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证照号码" prop="zzh">
          <el-input v-model="form.zzh" placeholder="请输入证照号码" />
        </el-form-item>
        <el-form-item label="主要办事机构所在地" prop="zybsjgszd">
          <el-input v-model="form.zybsjgszd" placeholder="请输入主要办事机构所在地" />
        </el-form-item>
        <el-form-item label="法定代表人姓名" prop="fddbr">
          <el-input v-model="form.fddbr" placeholder="请输入法定代表人姓名" />
        </el-form-item>
        <el-form-item label="法定代表人职务" prop="fddbrzw">
          <el-input v-model="form.fddbrzw" placeholder="请输入法定代表人职务" />
        </el-form-item>
        <el-form-item label="证件类型" prop="zjlx">
          <el-select v-model="form.zjlx" placeholder="请选择证件类型">
            <el-option
              v-for="dict in dict.type.fs_gw_zjlx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="zjhm">
          <el-input v-model="form.zjhm" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="手机号码" prop="sjhm">
          <el-input v-model="form.sjhm" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="固定电话" prop="gddh">
          <el-input v-model="form.gddh" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="创建时间" prop="cjsj">
          <el-input v-model="form.cjsj" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClient, getClient, delClient, addClient, updateClient } from "@/api/fs/client";

export default {
  name: "Client",
  dicts: ['fs_gw_zjlx', 'fs_gw_dsrlx', 'fs_gw_dzlx', 'fs_gw_dwxz', 'sys_user_sex', 'fs_gw_zzlx', 'fs_gw_zy'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 当事人表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dsrlx: null,
        xm: null,
        dz: null,
        dwmc: null,
        zy: null,
        dwxz: null,
        zzh: null,
        zybsjgszd: null,
        zjhm: null,
        cjsj: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询当事人列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then(response => {
        this.clientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        localId: null,
        bh: null,
        dsrlx: null,
        xm: null,
        xb: null,
        nl: null,
        csrq: null,
        dzlx: null,
        dz: null,
        dwmc: null,
        zy: null,
        zymc: null,
        gzdw: null,
        dwxz: null,
        zzlx: null,
        zzh: null,
        zybsjgszd: null,
        fddbr: null,
        fddbrzw: null,
        zjlx: null,
        zjhm: null,
        sjhm: null,
        gddh: null,
        cjsj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加当事人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClient(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改当事人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除当事人编号为"' + ids + '"的数据项？').then(function() {
        return delClient(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fs/client/export', {
        ...this.queryParams
      }, `client_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
